/* $Header: diag_ui.c,v 2.2 01/10/02 10:26:10 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  diag_ui.c                                             */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To provide the user interface functions for the    */
/*                  diagnostics.                                       */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <config.h>
#include <zone_status.h>
#include <status.h>
#include <diag.h>
#include <i_op.h>
#include <cmd_set.h>
#include <mio.h>
#include <method.h>
#include <hostutil.h>
#include <display.h>

#include <sig_types.h>
#include <sigpath.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

TEST_TYPE GetDiagTestType( void )
{
   return( Diag_State.type );
}

DIAG_STATUS GetDiagStatus( void )
{
   return( Diag_State.status );
}

void SetDiagStatus( DIAG_STATUS status )
{
   INTERRUPTS_OFF
      Diag_State.status = status;
   INTERRUPTS_ON
}

void SetDiagResult( DIAG_RESULT result )
{
   Diag_State.result = result;
}

DIAG_RESULT GetDiagResult( void )
{
   return( Diag_State.result );
}

/***********************************/
/* Serial Number Support Functions */
/***********************************/

const BIT8 INST_COUNTRY[6][2] =
{
   'U','S',
   'D','E',
   'B','R',
   'C','N',
   'I','N',
   'J','P'
};

INT16 GetSerialNumber( BIT8 *serial_no )
{
   U_INT32 number;

   if (( Serial_No.country >= UNKNOWN_COUNTRY ) ||
       ( Serial_No.country < US ))                   /* USA is 0, minimum # */
   {
      /* Set default serial number */
      serial_no[0] = 'U';
      serial_no[1] = 'S';
      number = 0;
   }
   else
   {
      /* Convert the country code to a string */
      serial_no[0] = INST_COUNTRY[(BIT8)Serial_No.country][0];
      serial_no[1] = INST_COUNTRY[(BIT8)Serial_No.country][1];
      number = Serial_No.number;
   }

   /* Next add the stored serial number, zero filled */
   (void)FormatDecimal( (BIT8 *)&serial_no[2], number, 0, (U_INT8)-8, 0 );

  /* Append a NULL */
   serial_no[10] = 0;

   return( 10 );
}

COUNTRY GetSerialNoCountry( BIT8 *str )
{
   INT16   country_no;
   COUNTRY country;

   country_no = ((INT16)*str)<<8;
   str++;
   country_no |= (INT16)*str;

   switch( country_no )
   {
      case MAKEINT('D','E'):
           country = GERMANY;
           break;
      case MAKEINT('B','R'):
           country = BRAZIL;
           break;
      case MAKEINT('C','N'):
           country = CHINA;
           break;
      case MAKEINT('I','N'):
           country = INDIA;
           break;
      case MAKEINT('J','P'):
           country = JAPAN;
           break;
      case MAKEINT('U','S'):
           country = US;
           break;
      default:
           country = UNKNOWN_COUNTRY;
   }

   return( country );
}


UI_ERR SetSerialNumber( BIT8 *serial_no )
{
   INT16 i;
   COUNTRY country;
   INT32 number;
   UI_ERR  check_no;

   /* Check to see if the serial number is of the correct length */
   i = StrLen( serial_no );

   if ( i < 10 ) return( PARAM_TOO_SMALL );
   if ( i > 10 ) return( PARAM_TOO_LARGE );

   /* Check to see if the serial number is of the correct format:

      XXDDDDDDDD, where XX is a 2-character alpha country code and
                  DDDDDDDD is an 8-digit number with zero fill if needed */

   country = GetSerialNoCountry( serial_no );
   check_no = DecToInt( serial_no+2, 0, &number );

   if (( country != UNKNOWN_COUNTRY ) && ( check_no == OK ))
   {
      INTERRUPTS_OFF
         Serial_No.country = country;
         Serial_No.number  = number;
      INTERRUPTS_ON

      return( OK );
   }

   return( INVALID_PARAM );

}



/***********************************************************************/
/* FUNCTION:  GetDaysHoursMinutesSeconds                               */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To convert a value of seconds into days, hours, minutes,  */
/*           and seconds.                                              */
/*                                                                     */
/* RETURNED VALUE:  TIME                                               */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

TIME GetDaysHoursMinutesSeconds( U_INT32 seconds )
{
   TIME  time;
   INT32 rem;

   time.days    = seconds/(24*60*60);
   rem          = seconds%(24*60*60);
   time.hours   = rem/(60*60);
   rem          = rem%(60*60);
   time.minutes = rem/60;
   time.seconds = rem%60;
   return(time);
}

/*****************************/
/* Version Support Functions */
/*****************************/

INT16 TransferVersion( BIT8 *line )
{
   REV_CODE version;

   version = Version;
   *line++ = version.version;
   *line++ = '.';

   /* Check that the update and fix are less than 100,       */
   /* otherwise the data are invalid.                        */
   if ( version.update > 99 ) version.update = 0;
   if ( version.fix    > 99 ) version.fix    = 0;

   /* Now get the update and fix values and convert to ascii */

   (void)IntToDec( version.update/10, line++ );
   (void)IntToDec( version.update%10, line++ );
   *line++ = '.';
   (void)IntToDec( version.fix/10, line++ );
   (void)IntToDec( version.fix%10, line++ );

   *line = 0;

   return 7;    /* length of the string */
}

/*******************************/
/* Instrument status functions */
/*******************************/

UI_ERR SetUptime( INT32 time )
{
   if ( time >= 0 )
   {
      Uptime_Clock = time;
      return OK;
   }

   return INVALID_PARAM;
}

UI_ERR SetRunCount( INT32 count )
{
   if ( count >= 0 )
   {
      Run_Count = count;
      return OK;
   }

   return INVALID_PARAM;
}

INT32 GetWhenManufacturedDate( void )
{
   return When_Manufactured.day*10000 +
          When_Manufactured.month*100 + When_Manufactured.year;
}

INT32 Get5VMonitorInmV( void )
{
   return ConvertToMv(Mon_5V_Rdg, (pPLOT_CONSTANTS)&MON_5V_CONSTS );
}

INT32 Get15VposMonitorInmV( void )
{
   return ConvertToMv(Mon_15Vpos_Rdg, (pPLOT_CONSTANTS)&MON_15VPOS_CONSTS);
}

INT32 Get15VnegMonitorInmV( void )
{
   return ConvertToMv(Mon_15Vneg_Rdg, (pPLOT_CONSTANTS)&MON_15VNEG_CONSTS);
}

INT32 Get24VMonitorInmV( void )
{
   return ConvertToMv(Mon_24V_Rdg, (pPLOT_CONSTANTS)&MON_15VPOS_CONSTS);
}

INT32 GetZoneVoltageIn10mV( void )
{
   return ( Zone_Status.secondary_voltage );
}

